home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 016a / gofer221.zip / CH08 < prev    next >
Text File  |  1991-11-20  |  4KB  |  133 lines

  1.  
  2.  
  3. Introduction to Gofer         8. ERRORS                                         
  4.  
  5.  
  6. 8. ERRORS
  7.  
  8. 8.1  Errors detected on input
  9. -----------------------------
  10. After an expression has been entered, but before any attempt is made to
  11. evaluate it, Gofer carries out a number of checks to make sure that the
  12. expression that you typed does not contain any errors.  Here  are  some
  13. examples of the kind of problem that might occur:
  14.  
  15.   o  Syntax errors.  The most common situation in which this happens is
  16.      when  you  make  a  typing  mistake,  either  leaving   out   some
  17.      characters, or perhaps pressing the wrong keys  instead.   In  the
  18.      following example, the user has missed out a `[' character:
  19.  
  20.          ? sum 1..100]
  21.          ERROR: Syntax error in input (unexpected `..')
  22.          ?
  23.  
  24.   o  Undefined variables.  This happens when you  enter  an  expression
  25.      using a variable or function name that is not defined  in  any  of
  26.      the files of definitions loaded into Gofer.  This can  often  mean
  27.      that you have misspelt the name of a function, or that  the  files
  28.      defining a function have not yet been loaded.  For example:
  29.  
  30.          ? sum [1..n]
  31.          ERROR: Undefined variable "n"
  32.          ? 
  33.  
  34.   o  Type errors.  Certain  expressions  are  only  sensible  when  the
  35.      functions used in those expressions are applied to values  of  the
  36.      appropriate type.  For example, whilst the factorial function  can
  37.      be used to calculate the factorial of an integer,  it  is  clearly
  38.      meaningless to try to  determine  the  factorial  of  a  character
  39.      value.  This kind of problem can be detected using  the  types  of
  40.      the components of an expression.  In the expression "fact 'A'", we
  41.      can see that the argument 'A' has type Char which does  not  match
  42.      the argument type Int of the factorial function.  This error  will
  43.      be detected by Gofer if you try to evaluate the expression:
  44.  
  45.          ? fact 'A'
  46.          ERROR: Type error in application
  47.          *** expression     : fact 'A'
  48.          *** term           : 'A'
  49.          *** type           : Char
  50.          *** does not match : Int
  51.  
  52.          ?
  53.  
  54.  
  55. 8.2  Errors during evaluation
  56. -----------------------------
  57. If no errors are detected in an input expression, Gofer then begins  to
  58. evaluate that expression.  Despite all of the checks that  are  carried
  59. out before the evaluation begins, it is still possible for an error  to
  60. occur during the evaluation of an expression.   A  typical  example  of
  61. this is an attempt to divide a number by zero.   In  this  case,  Gofer
  62.  
  63.  
  64.                                       19
  65.  
  66.  
  67.  
  68.  
  69. Introduction to Gofer         8.2  Errors during evaluation                     
  70.  
  71.  
  72. prints the part of the  expression  being  evaluated  that  caused  the
  73. error, surrounded by braces `{' and `}':
  74.  
  75.     ? 3/0
  76.     {primDivInt 3 0}
  77.     (4 reductions, 30 cells)
  78.     ? 
  79.  
  80. [The function "primDivInt" which appears here is a  primitive  function
  81. used to divide  one  integer  (its  first  argument)  by  another  (the
  82. second)].  Even if an error occurs in just one part of  an  expression,
  83. only the part causing the problem will be displayed:
  84.  
  85.     ? 4 + (5/0)
  86.     {primDivInt 5 0}
  87.     (5 reductions, 32 cells)
  88.     ? 
  89.  
  90. A standard function called "error" is defined in the  standard  prelude
  91. which is often useful for ensuring that appropriate error messages  are
  92. produced when an error occurs:
  93.  
  94.     ? error "Problem has occurred"
  95.     {error "Problem has occurred"}
  96.     (23 reductions, 99 cells)
  97.     ? 
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.                                       20
  131.  
  132.  
  133.